<?php
/**
 * 我的钱包模块
 * 2019.3.11
 * xuxh
 */
namespace app\api\controller;

use app\api\service\RegisterService;
use app\api\validate\RegisterValidate;
use app\api\validate\WalletValidate;
use think\Config;
use think\Db;

class WalletController extends BaseController
{

	/**
	 * 提现申请接口
     * @param money           提现金额
     * @param pay_password    支付密码
     * 提现收取手续费0.1%
	 */
	public function cash()
	{
        $this->checkUser();
	    $data = $this->request->post();
	    $data['user_id'] = $this->user_id;
        $user = Db::name('shop_user')->where('user_id',$data['user_id'])->find();
        $data['pay_code'] = $user['pay_code'];
        $data['balance'] = $user['balance'];
        $order_sn = cmf_get_order_sn();
	    $walletValid = new WalletValidate();
        $result = $walletValid->check($data);
        if(!$result){
            $this->ApiResult(false,10001,$walletValid->getError());
        }
        $cashFee = Config::get('cashFee');   //提现手续费费率
        $fee_money = number_format(($cashFee * $data['money']),2);
        $info = array('user_id'=>$this->user_id,'money'=>$data['money'],'fee_money'=>$fee_money,'create_time'=>time(),'card_id'=>$data['card_id'],'order_sn' => $order_sn);
        $rs = Db::name('cash_record')->insertGetId($info);
        if($rs){
            //加明细
            Db::name('balance_record')->insert(['money'=>$info['money'],'scene'=>1,'sign'=>2,'create_time'=>date('Y-m-d H:i:s'),'user_id'=>$info['user_id'],'remarks'=>'申请提现','data_id'=>$rs]);
            //更新用户的余额和冻结余额
            $new_balance = $user['balance'] - $data['money'];
            $new_freeze_balance = $user['freeze_balance'] + $data['money'];
            $user_info = ['balance'=>$new_balance,'freeze_balance'=>$new_freeze_balance];
            Db::name('shop_user')->where('user_id',$this->user_id)->update($user_info);
            $this->ApiResult(true,10000,'申请成功');
        }else{
            $this->ApiResult(false,10001,'申请失败');
        }
	}


}